package com.item.action;

import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.action.CoreController;
import com.base.dao.model.Ret;
import com.base.mobile.MobileData;
import com.base.mobile.server.annotation.MobileMethod;
import com.base.pay.PayMethod;
import com.base.pay.acp.UpmpBase;
import com.base.pay.wx.WxPrepay;
import com.base.util.GetuiPushUtil;
import com.base.util.JSONUtils;
import com.item.dao.model.Focus;
import com.item.dao.model.News;
import com.item.dao.model.SinglePage;
import com.item.service.FocusService;
import com.item.service.NewsService;
import com.item.service.SinglePageService;
import com.item.service.SmsSendLogService;
import com.social.dao.model.ForumAd;
import com.social.service.ForumAdService;
import com.weixin.dao.model.Article;
import com.weixin.service.ArticleService;

@Controller
public class SystemController extends CoreController {
	@Autowired
	private SinglePageService singlePageService;
	@Autowired
	private FocusService focusService;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ForumAdService adService;
	@Autowired
	private SmsSendLogService sendLogService;
	@Autowired
	private NewsService newsService;

	/**
	 * webview
	 * @param id
	 * @param type 类型
	 */
	@RequestMapping("/detail")
	public String detail(String id, Integer type, ModelMap map) throws Exception {
		if (StringUtils.isBlank(id) || type == null) {
			return null;
		}
		Map<String,Object> ret = new HashMap<String,Object>();
		switch (type) {
		case 1:
		case 2:
			// type==2 焦点图
			Focus focus = focusService.selectByPrimaryKey(id);
			if (focus != null) {
				ret.put("content", focus.getContent());
				map.put("obj", ret);
			} else {
				return "common/404";
			}
			break;
		case 3:
			// type==3 微信详情页
			Article article = articleService.selectByPrimaryKey(id);
			if (article != null) {
				ret.put("content", article.getContent());
				map.put("obj", ret);
			} else {
				return "common/404";
			}
			break;
		case 8:
			// type==8 新闻
			News obj = newsService.selectByPrimaryKey(id);
			if (obj != null) {
				ret.put("content", obj.getContent());
				map.put("obj", ret);
			} else {
				return "common/404";
			}
			break;
		case 9:
			// type==9 论坛推广
			ForumAd ad = adService.selectByPrimaryKey(id);
			if (ad != null) {
				ret.put("content", ad.getContent());
				map.put("obj", ret);
			} else {
				return "common/404";
			}
			break;
		default:
			break;
		}
		return "common/detail";
	}

	@RequestMapping("/singlePage/{code}")
	public String detail(@PathVariable("code")String code,ModelMap map) throws Exception {
		if (StringUtils.isBlank(code)) {
			return null;
		}
		SinglePage page = singlePageService.selectByPrimaryKey(code);
		if(page == null) page = new SinglePage();
		map.put("obj", page);
		return "common/detail";
	}
	
	@RequestMapping("/push")
	@ResponseBody
	public String push(String msg) throws Exception {
		if (StringUtils.isBlank(msg)) {
			return JSONUtils.serialize(new Ret(1, "请输入推送内容"));
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", 1);
		params.put("msg_android", msg);
		String tag = "";
		GetuiPushUtil.pushMessageToApp(params, msg, tag, null);
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/upmpPay")
	public void upmpPay(String oid, HttpServletResponse response) throws Exception {
		PrintWriter writer = response.getWriter();
		String html = UpmpBase.upmpWebPurchase(new BigDecimal(0.01), oid);
		writer.write(html);
		writer.flush();
		writer.close();
	}
	
	@RequestMapping("/wxPay")
	@ResponseBody
	public String wxPay(String oid) throws Exception {
		String url = WxPrepay.wxCodeUrl(new BigDecimal(0.01), oid, "微信支付", "8.8.8.8");
		if (url == null){
			url =  "http://www.baidu.com";
		}
		return JSONUtils.serialize(new Ret(1, url));
	}
	
	@RequestMapping("/aliPay")
	public void aliPay(String oid, HttpServletResponse response) throws Exception {
		PrintWriter writer = response.getWriter();
		String html = PayMethod.aliWebPay(oid, new BigDecimal(0.01), "测试订单","frontUrl");
		writer.write(html);
		writer.flush();
		writer.close();
	}
	
	@Transactional
	@RequestMapping("/getMsg")
	public void getMsg(String phone) throws Exception {
		String code= sendLogService.sendIdentifySms(phone, "1", "1", "1");
		System.out.println(code+">>>>>>");
	}
	
	@RequestMapping("/getApiList")
	@ResponseBody
	public String getApiName() throws Exception {
		Set<String> names = MobileData.getMethodNames();
		List<String> list = new ArrayList<String>(names.size());
		for (String name : names){
			MobileMethod method = MobileData.getMobileMethod(name);
			list.add(method.methodno());
		}
		return JSONUtils.serialize(list);
	}
}